# -*-coding:utf-8-*-
'''
File Create by Author:Backpackerxl
Create File Time:2021/10/13/013 21:00
Program IDE:PyCharm
File Name:UserDao.py
  
'''
from entity.BankUser import BankUser
from util.File import File
from util.UserLogin import UserLogin as user_login
from util.WatchInput import WatchInput as watch_input

user_data = File("./data/users.json").get_all_data()


class UserDao:
    def insert_user(self):
        info = []
        user = BankUser(self, None, None, None, None, None, None)
        user.set_user_name(watch_input.WatchInput(self, "请输入户主"))
        user.set_user_sex(watch_input.WatchInput(self, "请输入性别"))
        u_phone = watch_input.WatchInput(self, "请输入手机号")
        u_id = watch_input.WatchInput(self, "请输入身份证号")
        res = [phone for phone in user_data if u_phone == phone["user_phone"]]
        res1 = [userid for userid in user_data if u_id == userid["user_id"]]
        user.set_user_phone(u_phone) if len(res) == 0 else user.set_user_phone(None)
        user.set_user_id(u_id) if len(res1) == 0 else user.set_user_id(None)
        user.set_user_money(watch_input.WatchInput(self, "请输入预存金额"))
        u_passwd = watch_input.WatchInputPwd(self, "请输入密码")
        u_ispasswd = watch_input.WatchInputPwd(self, "请确认密码")
        user.set_user_password(u_passwd) if u_passwd == u_ispasswd else user.set_user_password(None)

        if user.get_user_id() == None:
            info.append("身份证号已被注册")
        if user.get_user_phone() == None:
            info.append("手机号已被注册")
        if user.get_user_password() == None:
            info.append("两次密码不一致")

        if user.get_user_id() == None or user.get_user_phone() == None or user.get_user_password() == None:
            user = None

        return user, user_data, info

    def look_money(self):
        res = user_login.three_login(self, user_data)
        return res

    def get_user(self):
        res = user_login.three_login(self, user_data)
        return res, user_data

    def find_by_user_bank_id(self, user_bank_id):
        res = [user for user in user_data if user_bank_id == user["user_bank_id"]]
        user = None
        if len(res) == 0:
            print("账户错误")
        else:
            user = BankUser(res[0]["user_name"],
                            res[0]["user_sex"],
                            res[0]["user_id"],
                            res[0]["user_phone"],
                            res[0]["user_money"],
                            res[0]["user_password"],
                            res[0]["user_bank_id"],
                            res[0]["user_use_state"], )
        return user, user_data

    def preservation_all_data(self):
        File("./data/users.json").preservation_data(user_data)
